
do "...\First.do"



********************************************************************************
* Merge background characteristics
********************************************************************************

clear 
use "$raw\patient_background_moreyears.dta"

keep if inrange(year,1995,2019)
rename alder age
drop if age<20

drop if pnr=="" 

format koen %1.0f
format ie_type %1.0f

g male=(koen==1)
g dk=(ie_type==1)
g yob= year-age
g married=(civst=="G")

g non_dk=(dk!=1)

keep pnr age male dk yob kom year married ie_type non_dk


* Recode municipality code to new codes (there was a reform in 2007)
* We use the new municodes in the entire project to avoid databreaks.
do "$do\z_muni_codes.do"

********************************************************************************
* Add patient education
********************************************************************************
merge m:1 pnr using "$work\children3.dta"
drop if _merge==2 
drop _merge

bys pnr year: g N=_N

g low_ses=(udd_level=="10")
g ses_missing=(udd_level=="")



********************************************************************************
* Add GP services
********************************************************************************

merge 1:1 pnr year using "$work\gp_services.dta"
drop if _merge==2
drop _merge


foreach var of varlist n_visits diabetes_control services {
	replace `var'=0 if `var'==.
	replace `var'=. if year<1990
	
}

replace diabetes_control=. if !inrange(year,2006,2011)

g GP_visit=(n_visits>0)
tab year GP_visit, row
tab GP_visit

replace services=. if n_visits==0

rename n_visits N_visits2


********************************************************************************
* Add costs
********************************************************************************

merge 1:1 pnr year using "$work\costs2.dta"
drop if _merge==2
drop _merge


replace total_cost=0 if total_cost==.


********************************************************************************
* Add medication
********************************************************************************

merge 1:1 pnr year using "$work\lmdb_new_types.dta"
drop if _merge==2
drop _merge

foreach var of varlist statins ACE metformin COPD_med {
	
	replace `var'=0 if `var'==.
	replace `var'=. if year<1995
	replace `var'=. if year>2019
	}



********************************************************************************
* Merge death and cause of death
********************************************************************************

merge m:1 pnr using "$work\deaths.dta"
drop if _merge==2
drop _merge

g death=(year==yod)
tab year death
replace death=. if year==2021
replace death=. if year<1995

foreach var of varlist death_CVC-death_COPD {
	replace `var'=0 if death==0
	replace `var'=0 if `var'==.
}


********************************************************************************
* Lung scans
********************************************************************************

merge 1:1 pnr year using  "$work\lung_scans.dta"
drop if _merge==2
drop _merge 

replace lungscan=0 if lungscan==.
replace first_lung=0 if first_lung==.

replace first_lung=. if year<2002
replace first_lung=. if year>2018

replace lungscan=. if year<2002
replace lungscan=. if year>2018


********************************************************************************
* Hospital admissions 
********************************************************************************

merge 1:1 pnr year using "$work\hospital_admissions.dta"
drop if _merge==2
drop _merge


replace ACSC_COPD=0 if ACSC_COPD==.
replace ACSC_COPD=. if year>2018
	

********************************************************************************
********************************************************************************

label var year "Year"
label var kom "Municipality code"
label var male "Male"
label var dk "Ethnical Danish"
label var married "Married"
label var age "Age"
label var udd_level "Education code"
label var udd "Education groups"

label var low_ses "High school"
label var ses_missing "Missing information about education"
label var death_stroke "Death from cardiovascular disease"
label var yod "Year of death"
label var death "Death"

rename d_statdato date_death
label var date_death "Death date"
label var death_stroke_acute "Death from acute stroke"




********************************************************************************
********************************************************************************
********************************************************************************

sort pnr year

save "$work\patient_background.dta", replace

